package com.project.communitymanagement.auth.common;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.log4j.Log4j2;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @ProjectName: community-management
 * @Package: com.project.communitymanagement.auth.common
 * @ClassName: AuthenticationEntryPointUser
 * @Author: "XGP"
 * @Description:
 * @Date: 2022/3/13 15:55
 * @Version: 1.0
 */
@Component
@Log4j2
public class AuthenticationEntryPointUser implements AuthenticationEntryPoint {
    private Logger logger = LoggerFactory.getLogger(getClass());
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        logger.info("账号未登陆,{}");
        JSONObject jsonObject = new JSONObject();
        String message =  "账号未登陆！";
        jsonObject.put("code", 410);
        jsonObject.put("message", "SUCCESS");
        jsonObject.put("data", message);
        response.setContentType("application/json;charset=UTF-8");
        response.getWriter().write(jsonObject.toJSONString());
    }
}
